/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.cms.entity.CategoryRole;
import cn.net.ecode.modules.cms.service.CategoryRoleService;
import cn.net.ecode.modules.sys.entity.Role;
import cn.net.ecode.modules.sys.service.RoleService;

/**
 * 栏目授权Controller
 * @author jiangwenchao
 * @version 2014-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/categoryRole")
public class CategoryRoleController extends BaseController {
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private CategoryRoleService categoryRoleService;
	
	/**
	 * 查询role列表
	 * @param role
	 */
	@RequiresPermissions("cms:categoryRole:view")
	@RequestMapping(value = {"list", ""})
	public String list(Role role, Model model) {
		return "modules/cms/categoryRoleList";
	}
	
	/**
	 * 查询role列表
	 * @param role
	 * @return
	 */
	@RequiresPermissions("cms:categoryRole:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<Role> listData(Role role, HttpServletRequest request, HttpServletResponse response) {
		Page<Role> page = roleService.findPage(new Page<Role>(request, response), role);
		return page;
	}

	/**
	 * 查看编辑表单
	 * @param categoryRole
	 * @param model
	 * @return
	 */
	@RequiresPermissions("cms:categoryRole:view")
	@RequestMapping(value = "form")
	public String form(CategoryRole categoryRole, Model model) {
			//	获取所有站点下的栏目和所传递的角色下有哪些授权栏目
		categoryRole.setRole(roleService.get(categoryRole.getRole()));
		categoryRole=categoryRoleService.getSiteCategoryRole(categoryRole);
		model.addAttribute("categoryRole", categoryRole);
		return "modules/cms/categoryRoleForm";
	}

	/**
	 * 保存数据
	 * @param categoryRole
	 * @return
	 */
	@RequiresPermissions("cms:categoryRole:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(CategoryRole categoryRole) {
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, categoryRole)){
			return renderResult(Global.FALSE, message);
		}
		categoryRoleService.save(categoryRole);
		return renderResult(Global.TRUE, "保存栏目授权成功！");
	}
	
	/**
	 * 清空该角色下的所有栏目授权
	 * @param categoryRole
	 * @return
	 */
	@RequiresPermissions("cms:categoryRole:delete")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String deleteByRole(Role role,  RedirectAttributes redirectAttributes) {
		int i=categoryRoleService.deleteByRole(new CategoryRole(role));
		return renderResult(Global.TRUE, "删除授权"+i+"个，清空该角色下授权成功！");
	}

}